// pages/my/my.js
const app = getApp()
const req = require('../../utils/request.js')
let aa=true;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    conent_show_love:true,
    conent_show:false,
    currentTab:0,
    name:true,
    window_height:"",
    login:false,
    loginInfo: {
      title: '微信授权',
      content: '获得您的公开信息(昵称、头像等)',
      logName: 'Gayhub',
      logImage: '../../img/logo.jpg',

    },
    login_info:{},
    content: [

    ],

    tab:[
      {
        title:"浏览记录",

      },
      {
        title: "我的收藏",

      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.mylook();
  },
  getToken() {
    wx.login({
      success: res => {
        wx.request({
          url: 'https://factory.yummyn.com/api/v1/user/getLoginToken',
          data: {
            code: res.code
          },
          header: {
            'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
          },
          method: 'POST',
          success: function (res) {
            wx.setStorageSync('token', res.data.result.token);
          }
        })
      }
    })
  },
  onConfirm(e) { // 点击允许
  let self=this
    let dialogComponent = this.selectComponent('.wxc-dialog');
    dialogComponent && dialogComponent.hide();
    let userInfo = JSON.parse(e.detail.detail.rawData)
    if (!userInfo) {
      return;
    }
    this.setData({
      login_info: userInfo
    })
    this.getToken()
    setTimeout(()=>{
      self.mylook()
    },1000)
    wx.setStorageSync('userInfo', userInfo)
  },
  onCancel() { // 点击拒绝
    let dialogComponent = this.selectComponent('.wxc-dialog');
    dialogComponent && dialogComponent.hide();
  },
  login(){
    let dialogComponent = this.selectComponent('.wxc-dialog');
    dialogComponent && dialogComponent.show();
  },
  clear(){
wx.clearStorage()
this.onShow()
  },
  onShow() {
    this.setData({
      login_info: wx.getStorageSync("userInfo")
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  mylove(){
    let that = this
    wx.showLoading({
      title: '加载中',
    })
    req.networkget("v1/collect/getUserCollects",{page:1,size:29}).then(res => {
      if (res.data.result_code == 200) {
        that.setData({
          content: res.data.result
        })
       wx.hideLoading()
       console.log(this.data.content,"2")
      } else {
        wx.showToast({
          title: res.data.message,
        })
      }

    }).catch(res => {
      wx.showToast({
        title: "请求失败"
      })
    })
  },

  mylook() {
    let that = this
    wx.showLoading({
      title: '加载中',
    })
    req.networkget("v1/history/getUserHistory", { page: 1, size: 29 }).then(res => {
      if (res.data.result_code == 200) {
        that.setData({
          content: res.data.result
        })
        
       wx.hideLoading()
      } else {
        wx.showToast({
          title: res.data.message,
        })
      }

    }).catch(res => {
      wx.showToast({
        title: "请求失败"
      })
    })
  },
  swichNav:function (e) {
    let cur = e.target.dataset.current;
      this.setData({
        content:[]
      })
    if (this.data.currentTab == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur
      })
    }
   if(cur==1){
     this.mylove()
     this.setData({
       conent_show:true,
       conent_show_love:false
     })
   }else{
     this.mylook()
     this.setData({
       conent_show: false,
       conent_show_love: true
     })
   }
  },
  /**
   * 生命周期函数--监听页面显示
   */


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})